package com.hhxy.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.hhxy.model.dto.AddArticleDto;
import com.hhxy.service.ArticleService;
import com.hhxy.utils.response.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * @author ghp
 * @date 2023/5/1
 * @title
 * @description
 */
@Api(tags = {"后端接口", "文章管理", "内容管理"})
@ApiSupport(author = "ghp", order = 1)
@RestController
@RequestMapping("/content/article")
@Validated
public class BeArticleController {

    @Resource
    private ArticleService articleService;

    @GetMapping("/list")
    @ApiOperation(value = "分页条件查询文章")
    @ApiOperationSupport(author = "ghp", order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页码", dataTypeClass = Integer.class, required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页大小", dataTypeClass = Integer.class, required = true),
            @ApiImplicitParam(name = "title", value = "文章标题"),
            @ApiImplicitParam(name = "summary", value = "文章摘要")
    })
    public ResponseResult list(@NotNull(message = "pageNum不能为空") Integer pageNum,
                               @NotNull(message = "pageSize不能为空") Integer pageSize,
                               String title, String summary) {
        return articleService.list(pageNum, pageSize, title, summary);
    }

    @PostMapping
    @ApiOperation(value = "新增文章")
    @ApiOperationSupport(author = "ghp", order = 1)
    public ResponseResult add(@RequestBody @Valid AddArticleDto addArticleDto) {
        return articleService.add(addArticleDto);
    }

    @DeleteMapping("/{ids}")
    @ApiOperation(value = "根据id删除文章")
    @ApiOperationSupport(author = "ghp", order = 1)
    public ResponseResult deleteArticleByIds(@PathVariable @NotNull(message = "id不能为空") Long[] ids) {
        return articleService.deleteArticleByIds(ids);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询文章")
    @ApiOperationSupport(author = "ghp", order = 1)
    @ApiImplicitParam(name = "id", value = "文章id", dataTypeClass = Long.class, required = true)
    public ResponseResult getArticleById(@PathVariable @NotNull(message = "id不能为空") Long id) {
        return articleService.getArticleById(id);
    }

    @PutMapping
    @ApiOperation(value = "根据id修改文章")
    @ApiOperationSupport(author = "ghp", order = 1)
    public ResponseResult updateArticleById(@RequestBody @Valid AddArticleDto addArticleDto) {
        return articleService.updateArticleById(addArticleDto);
    }


}
